package com.cxdr.awsips.packing.file;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.cxdr.awsips.packing.bean.Target;
import com.cxdr.awsips.packing.config.SystemConfig;
import com.cxdr.awsips.packing.logger.LogFactory;

public class LocalFileHelper implements FileHelper {
	private static Logger logger = LogFactory.makeNewLogger(LocalFileHelper.class);
	private String remoteDir;
	private String localDir;
	private Target target;
	public LocalFileHelper(Target target){
		this.target = target;
		this.remoteDir =target.getPath();;
		if(StringUtils.isBlank(remoteDir)){
			this.remoteDir = SystemConfig.get("path.source");
		}
		this.localDir = SystemConfig.get("path.source");
	}
	@Override
	public Set<File> scanDir() {
		return scanDir(getRemoteDir());
	}

	@Override
	public Set<File> scanDir(String dir) {
		// TODO Auto-generated method stub
		return scanDir(dir, "txt");
	}

	@Override
	public Set<File> scanDir(String dir, String fileType) {
		String path = dir;
		logger.info("开始扫描目录" + path + "....");
		Set<File> set = new HashSet<File>();
		File fileDir = new File(path);
		if (!fileDir.isDirectory()) {
			logger.info("目录" + path + "不存在，请检查源目录");
			// FileScaner.this.cancel();
			return set;
		}
		File[] files = fileDir.listFiles();
		logger.info("扫描到 " + files.length + "个文件");
		if (files != null) {
			for (File file : files) {
				if (StringUtils.isBlank(fileType)) {
					File localfile = getFile(file.getName());
					if(localfile!=null){
						logger.debug("添加文件:"+localfile.getAbsolutePath());
						set.add(localfile);
					}
					continue;
				}
				if (StringUtils.endsWithIgnoreCase(file.getName() ,"." + fileType)) {
					File localfile = getFile(file.getName());
					if(localfile!=null){
						logger.debug("添加文件:"+localfile.getAbsolutePath());
						set.add(localfile);
					}
				}
			}
		}
		return set;
	}

	public File getFile(String fileName) {
		File localFile = null;
		if(localDir.equals(remoteDir)){
			return new File(remoteDir + File.separator + fileName);
		}
		try{
			localFile =  new File(localDir + File.separator + fileName);
			File remoteFile = new File(remoteDir + File.separator + fileName);
			FileUtils.copyFile(remoteFile,localFile);
			if(target!=null){
				if(!StringUtils.isBlank(target.getBackpath())){
					File backcFile = new File(target.getBackpath()+File.separator+fileName);
					if(backcFile.isFile()){
						backcFile.delete();
					}
					logger.info("备份远程文件："+remoteFile.getAbsolutePath());
					if(!remoteFile.renameTo(backcFile)){
						remoteFile.delete();
					}
				}else{
					logger.info("删除远程文件："+remoteFile.getAbsolutePath());
					remoteFile.delete();
				}
			}
		}catch (Exception e){
			logger.error("复制文件错误",e);
			return null;
		}
		return localFile;
	}

	@Override
	public void open() throws IOException {
		// TODO Auto-generated method stub

	}

	public void getDir(String dir) {
		try{
			FileUtils.copyFile(new File(remoteDir), new File(localDir));
		}catch(Exception e){
		}
	}

	@Override
	public void close() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean renameFile(String from, String to) {
		boolean	result = false;
		try{
			result = new  File(remoteDir+File.separator+from).renameTo(new File(remoteDir+File.separator+to));
		}catch(Exception e){
		}
		return result;
	}

	@Override
	public boolean deleteFile(String fileName) {
		boolean	result = false;
		try{
			result = new  File(remoteDir+File.separator+fileName).delete();
		}catch(Exception e){
		}
		return result;
	}

	@Override
	public boolean uploadFile(String fileName) {
		try{
			FileUtils.copyFile(new File(localDir + File.separator + fileName), new File(remoteDir + File.separator + fileName));
		}catch (Exception e){
			return false;
		}
		return true;
	}

	@Override
	public void setRemoteDir(String remoteDir) {
		this.remoteDir = remoteDir;

	}

	@Override
	public void setLocalDir(String localDir) {
		this.localDir = localDir;
	}

	public String getRemoteDir() {
		return remoteDir;
	}
}
